
//var router = require('koa-router')({prefix: '/app'});
var router = require('koa-router')();
var controllers = require('../controllers');
var wx = require('../module/weixin');

// Broker
router.get('/broker/houses/:houseId', controllers.forBroker.getRoom);
router.get('/broker/find', controllers.forBroker.getFind);

// Client
router.get('/app/client/houses/:houseId', controllers.forClient.getRoom);
router.get('/app/client/communities/:communityId', controllers.forClient.getCommunity);
router.get('/app/client/users/:userId', controllers.forClient.getUser);
router.get('/app/client/chiefs/:chiefId', controllers.forClient.getChief);
router.get('/app/client/teams/:chiefId', controllers.forClient.getChief);
router.get('/app/client/publish', controllers.forClient.publishRoom);
router.post('/app/client/publish', controllers.forClient.postRoom);
router.get('/app/client/panoramic/:panoramicId', controllers.forClient.getPanoramic);
router.get('/app/client/about/fangmm', controllers.forClient.getFangmm);
router.get('/app/client/about/chief', controllers.forClient.getChies);
router.get('/app/client/guide', controllers.forClient.getGuide);
router.get('/app/client/provision', controllers.forClient.getProvision);
router.get('/app/client/feedback', controllers.forClient.getFeed);
router.get('/app/client/clear', function *() {
    this.session.Token = null;
    this.session.Ticket = null;
    this.body = JSON.stringify({
        code: 200,
        msg: 'ok'
    });
})

// weixin3.0
var infoUrl = '/node/wxAccessTokenBase';
var callUrl = '/node/wxLoadOpenidByCodeBase';
var host    = 'http://m.fangmm.com';
var doCheck = function (url) {
    return function *(next) {
        this.session.refer = url || '/';
        if(!this.session.User) {
            return this.redirect('/register');
        }
        yield next;
    }
};

var weixinCheck = function(url) {

    return function *(next) {
        this.session.wxRefer = url || '/';
        if(!this.session.Winxin) {
            return this.redirect(infoUrl);
        } else {
            yield next;
        }
    }
}

router.get('/', controllers.forClient.getList);
router.get('/requirement', doCheck('/requirement'), controllers.forClient.getRequirement);
router.get('/search', function *() {
    this.redirect('/');
});



router.get('/publish', doCheck('/publish'), function *() {
    this.redirect('/app/client/publish?userId=' + this.session.User.userId);
})

router.get('/register', controllers.forClient.getRegister);
router.get('/user/token/code', controllers.forClient.getCode);
router.post('/user/token/auth', controllers.forClient.getAuth);
router.get('/lists/:listId', controllers.forClient.getDetail);

// user info
router.get('/wx/requirement', weixinCheck('/requirement'), function *() {
   return this.redirect('/requirement');
})
router.get(infoUrl, wx.getUserInfo(host + callUrl));
router.get(callUrl, wx.updateUserInfo);


// Error
router.get('/404', function *(next) {
    yield this.render('404');
});
router.get('/error', function *(next) {
    yield this.render('error');
});


module.exports = router;